﻿Imports System.Windows.Forms

Namespace Controls
    ''' <summary> 
    ''' 属性页编辑窗体 
    ''' </summary> 
    Class TabEditorForm
        Inherits System.Windows.Forms.Form
        Private treeView1 As System.Windows.Forms.TreeView
        Private propertyGrid1 As System.Windows.Forms.PropertyGrid
        Private save As System.Windows.Forms.Button
        Private cancel As System.Windows.Forms.Button
        Private components As System.ComponentModel.IContainer
        Private button3 As System.Windows.Forms.Button

        Private _tabStrip As TabControl
        Private delnode As System.Windows.Forms.Button
        Private toolTip1 As System.Windows.Forms.ToolTip

        Public Tabs As TabPageCollection


        Private Sub LoadNodes(ByVal oItem As TabPage, ByVal oTreeNode As TreeNode)
            oTreeNode.Tag = oItem

            For Each oChild As TabPage In oItem.Controls
                Dim oChildNode As New TreeNode(oChild.Caption)
                LoadNodes(oChild, oChildNode)

                oTreeNode.Nodes.Add(oChildNode)
            Next
        End Sub

        Public Sub New(ByVal oTabStrip As TabControl)
            InitializeComponent()

            _tabStrip = oTabStrip
            Tabs = oTabStrip.Items

            ' 加载已添加的结点 
            For Each oRoot As TabPage In Tabs
                Dim oRootNode As New TreeNode(oRoot.Caption)
                LoadNodes(oRoot, oRootNode)
                treeView1.Nodes.Add(oRootNode)
            Next

            treeView1.HideSelection = False
        End Sub


        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If components IsNot Nothing Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

#Region "Windows Form Designer generated code"
        Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container()
            Me.treeView1 = New System.Windows.Forms.TreeView()
            Me.propertyGrid1 = New System.Windows.Forms.PropertyGrid()
            Me.save = New System.Windows.Forms.Button()
            Me.cancel = New System.Windows.Forms.Button()
            Me.button3 = New System.Windows.Forms.Button()
            Me.delnode = New System.Windows.Forms.Button()
            Me.toolTip1 = New System.Windows.Forms.ToolTip(Me.components)
            Me.SuspendLayout()
            ' 
            ' treeView1 
            ' 
            Me.treeView1.Location = New System.Drawing.Point(12, 44)
            Me.treeView1.Name = "treeView1"
            Me.treeView1.Size = New System.Drawing.Size(307, 310)
            Me.treeView1.TabIndex = 0
            AddHandler Me.treeView1.AfterSelect, AddressOf treeView1_AfterSelect
            ' 
            ' propertyGrid1 
            ' 
            Me.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar
            Me.propertyGrid1.Location = New System.Drawing.Point(336, 1)
            Me.propertyGrid1.Name = "propertyGrid1"
            Me.propertyGrid1.Size = New System.Drawing.Size(326, 353)
            Me.propertyGrid1.TabIndex = 1
            AddHandler Me.propertyGrid1.PropertyValueChanged, AddressOf propertyGrid1_ValueChanged
            ' 
            ' save 
            ' 
            Me.save.Location = New System.Drawing.Point(432, 371)
            Me.save.Name = "save"
            Me.save.Size = New System.Drawing.Size(106, 25)
            Me.save.TabIndex = 2
            Me.save.Text = " 保 存 "
            AddHandler Me.save.Click, AddressOf save_Click
            ' 
            ' cancel 
            ' 
            Me.cancel.Location = New System.Drawing.Point(557, 371)
            Me.cancel.Name = "cancel"
            Me.cancel.Size = New System.Drawing.Size(105, 25)
            Me.cancel.TabIndex = 3
            Me.cancel.Text = " 取 消 "
            AddHandler Me.cancel.Click, AddressOf cancel_Click
            ' 
            ' button3 
            ' 
            Me.button3.Location = New System.Drawing.Point(12, 1)
            Me.button3.Name = "button3"
            Me.button3.Size = New System.Drawing.Size(101, 34)
            Me.button3.TabIndex = 4
            Me.button3.Text = "添加属性页"
            Me.toolTip1.SetToolTip(Me.button3, "添加属性页")
            AddHandler Me.button3.Click, AddressOf button3_Click
            ' 
            ' delnode 
            ' 
            Me.delnode.Location = New System.Drawing.Point(130, 1)
            Me.delnode.Name = "delnode"
            Me.delnode.Size = New System.Drawing.Size(87, 34)
            Me.delnode.TabIndex = 6
            Me.delnode.Text = "删除属性页"
            Me.toolTip1.SetToolTip(Me.delnode, "删除属性页")
            AddHandler Me.delnode.Click, AddressOf delnode_Click
            ' 
            ' TabEditorForm 
            ' 
            Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
            Me.ClientSize = New System.Drawing.Size(683, 398)
            Me.Controls.Add(Me.delnode)
            Me.Controls.Add(Me.button3)
            Me.Controls.Add(Me.cancel)
            Me.Controls.Add(Me.save)
            Me.Controls.Add(Me.propertyGrid1)
            Me.Controls.Add(Me.treeView1)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
            Me.Name = "TabEditorForm"
            Me.Text = "LSW TabPage Designer"
            Me.ResumeLayout(False)

        End Sub
#End Region


        ' New root 
        Private Sub button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim oItem As New TabPage()
            oItem.Caption = "新属性页"

            Tabs.Add(oItem)

            Dim oNewTreeNode As New TreeNode("新属性页")
            oNewTreeNode.Tag = oItem
            treeView1.Nodes.Add(oNewTreeNode)

            treeView1.SelectedNode = treeView1.Nodes(treeView1.Nodes.Count - 1)
        End Sub




        ' 在结点上单击 
        Private Sub treeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs)
            propertyGrid1.SelectedObject = e.Node.Tag
        End Sub


        ' 删除结点 
        Private Sub delnode_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If treeView1.SelectedNode IsNot Nothing Then
                Dim oItem As TabPage = DirectCast(treeView1.SelectedNode.Tag, TabPage)

                _tabStrip.Items.Remove(oItem)
                treeView1.SelectedNode.Remove()
            End If
        End Sub

        Private Sub propertyGrid1_ValueChanged(ByVal sender As Object, ByVal e As PropertyValueChangedEventArgs)
            ' 如果已修改 text,则同时更新左侧树 
            'if (e.ChangedItem.Label == "Text") 
            '{ 
            ' treeView1.SelectedNode.Text = (string)e.ChangedItem.Value; 
            '} 

            If e.ChangedItem.Label = "Caption" Then
                treeView1.SelectedNode.Text = DirectCast(e.ChangedItem.Value, String)
            End If
        End Sub

        Private Sub save_Click(ByVal sender As Object, ByVal e As EventArgs)
            DialogResult = DialogResult.OK
            Close()
        End Sub

        Private Sub cancel_Click(ByVal sender As Object, ByVal e As EventArgs)
            DialogResult = DialogResult.Cancel
            Close()
        End Sub


    End Class
End Namespace